package visual;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import logic.Manager;

public class Frame extends JFrame implements ActionListener{
/*
 * Atributes
 */
	String MENU_STRINGS[] = {
			"Juego"
		};
	Object ITEM_STRINGS[] = {
			"Juego Nuevo",
			"Salvar Juego",
			"Cargar Juego",
			"Salir"
		};
	public Manager gameManager;
	public InternalFrame newGameFrame;
	public InternalFrame gameFlowFrame;
/*
 * Constructors
 */
	public Frame(String windowName) throws HeadlessException {
		super(windowName);
		getContentPane().setLayout(new FlowLayout ());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		createMenu();
		setSize(new Dimension(800, 600));
        setLocationRelativeTo(null);
	}

/*
 * Methods
 */
	public void close(){
		setVisible(false);
		dispose();
	}
	
	public void createAndShowGUI() {
        setLocationRelativeTo(null);
        setVisible(true);
	}
	
	private void createMenu(){
		JMenuBar menuBar;
		JMenu gameMenu;
		JMenuItem menuItem;
		menuBar = new JMenuBar();
		gameMenu = new JMenu(MENU_STRINGS[0]);
		gameMenu.setMnemonic(KeyEvent.VK_J);
		for(int i = 0; i < ITEM_STRINGS.length; i++){
			menuItem = new JMenuItem(ITEM_STRINGS[i].toString());
			if(i == ITEM_STRINGS.length - 1){
				gameMenu.addSeparator();
			}
			menuItem.addActionListener(this);
			gameMenu.add(menuItem);

		}
		menuBar.add(gameMenu);
		setJMenuBar(menuBar);
	}

	public void actionPerformed(ActionEvent event) {
		if(event.getActionCommand() == ITEM_STRINGS[0]){
			createNewGameFrame();
		}else if(event.getActionCommand() == ITEM_STRINGS[3]){
			close();
		}else if(event.getActionCommand() == "Comenzar"){
//			gameManager = new Manager();
			newGameFrame.dispose();
			validate();
		}
		System.out.println("event: " + event);
	}
	
	
	public void createNewGameFrame(){
		final String clientString = "Cliente";
		final String serverString = "Servidor";

		newGameFrame = new InternalFrame("Nuevo Juego"){ //Clase anonima de InternalFrame
			JTextField ip = new JTextField("255.255.255.255");
					
			public void actionPerformed(ActionEvent event) {
				if(lastCommand.matches(event.getActionCommand())) return; //SI el evento es el mismo no hacer nada.
				
				if(event.getActionCommand() == clientString){
					ip.setBorder(BorderFactory.createTitledBorder("Conectar a"));
					getContentPane().add(ip);
				} else if(event.getActionCommand() == serverString){
					ip.setText("255.255.255.255");
					getContentPane().remove(ip);
				}
				pack();
				lastCommand = event.getActionCommand();
			}
			
			public void createInternalFrame(){
				JTextField playerName = new JTextField("Ingresa tu nombre");
				playerName.setBorder(BorderFactory.createTitledBorder("Jugador"));
				setLayout(new FlowLayout());
				getContentPane().add(playerName);
				JRadioButton clientButton = new JRadioButton(clientString);
				clientButton.setActionCommand(clientString);
			    JRadioButton serverButton = new JRadioButton(serverString);
			    serverButton.setActionCommand(serverString);

			    ButtonGroup group = new ButtonGroup();
			    group.add(clientButton);
			    group.add(serverButton);
			    clientButton.addActionListener(this);
			    serverButton.addActionListener(this);
			    
		        JPanel radioPanel = new JPanel(new GridLayout(0, 1));
		        radioPanel.add(clientButton);
		        radioPanel.add(serverButton);
		        
		        getContentPane().add(radioPanel);
		        setVisible(true);				
			}
		};
		JButton startGame = new JButton("Comenzar");
		startGame.addActionListener(this);
		newGameFrame.getContentPane().add(startGame);
		getContentPane().add(newGameFrame);
		validate();
	}
	
	public void createGameFlowFrame(){
		gameFlowFrame = new InternalFrame("Tablero de juego"){
			
		};
	}
}
